//
//	HOME AUTOMATION GATEWAY PROJECT
//
//  (c) 2008 mocom software GmbH & Co KG
//	for European Microsoft Innovation Center
//
//  $Author: volker $
//  $Date: 2009-03-13 16:07:41 +0100 (Fr, 13. Mrz 2009) $
//  $Revision: 286 $
//
//  Microsoft dotNetMF Project
//  Copyright ©2001,2002,2003,2004 Microsoft Corporation
//  One Microsoft Way, Redmond, Washington 98052-6399 U.S.A.
//  All rights reserved.
//  MICROSOFT CONFIDENTIAL
//
//-----------------------------------------------------------------------------
#include <tinyhal.h>
#include <rtip.h>
#include <net_decl.h>
#include "AX88796B.h"
#include "hag_eth_driver.h"

#if defined(ADS_LINKER_BUG__NOT_ALL_UNUSED_VARIABLES_ARE_REMOVED)
#pragma arm section zidata = "g_AX88796B_Driver"
#endif

AX88796B_Driver g_AX88796B_Driver;

#if defined(ADS_LINKER_BUG__NOT_ALL_UNUSED_VARIABLES_ARE_REMOVED)
#pragma arm section zidata
#endif

#ifdef AX88796_IRQ_PIN
extern void AX88796B_interrupt(GPIO_PIN Pin, BOOL PinState, UINT32 Param);
#else
extern void AX88796B_interrupt(void *param);
#endif

extern int xn_bind_AX88796B_mac(int minor_number);

// -- //

BOOL Network_Interface_Bind(int index)
{
    return AX88796B_Driver::Bind();
}
int  Network_Interface_Open(int index)
{
    return AX88796B_Driver::Open();
}
BOOL Network_Interface_Close(int index)
{
    return AX88796B_Driver::Close();
}

int AX88796B_Driver::Open()
{
    int use_default_multicast;

	debug_printf("ETH: Start AX88796B Driver\r\n");


    memset(&g_AX88796B_Driver.m_currentDhcpSession, 0, sizeof(g_AX88796B_Driver.m_currentDhcpSession));


     /* Open the interface first */
    g_AX88796B_Driver.m_interfaceNumber = xn_interface_open_config(AX88796B_DEVICE,
                                                    0,              /*  minor_number        */
                                                    0,              /*  ioaddress           */
                                                    0,              /*  irq value           */
                                                    0               /*  mem_address)        */
                                                    );

    if (g_AX88796B_Driver.m_interfaceNumber == -1)
    {
        return -1;
    }

    use_default_multicast = 1;
    if (xn_interface_opt(g_AX88796B_Driver.m_interfaceNumber,
                        IO_DEFAULT_MCAST,
                        (RTP_PFCCHAR)&use_default_multicast,
                        sizeof(int)) == -1)
    {
         // Failed to set the default multicast interface */
        debug_printf("ETH: Failed to set the multicast = %s\r\n", use_default_multicast ? "ON" : "OFF" );
    }

    // Enable Interrupt-Pin and activate interrupt service
#ifdef AX88796_IRQ_PIN
	CPU_GPIO_EnableInputPin2(AX88796_IRQ_PIN,
						 FALSE,
						 (GPIO_INTERRUPT_SERVICE_ROUTINE)AX88796B_interrupt,
						 0,		// minor number
						 GPIO_INT_EDGE_LOW,
						 RESISTOR_PULLUP);
#else
	CPU_INTC_ActivateInterrupt(AX88796_IRQ_ID, AX88796B_interrupt, NULL);
#endif
    return g_AX88796B_Driver.m_interfaceNumber;
}

// -- //

BOOL AX88796B_Driver::Close(void)
{
    int retVal = -1;

#ifdef AX88796_IRQ_PIN
	CPU_GPIO_DisablePin( AX88796_IRQ_PIN,RESISTOR_PULLUP,0,GPIO_ALT_PRIMARY);
#else
	CPU_INTC_DeactivateInterrupt(AX88796_IRQ_ID);
#endif


    /* JRT - changed interface number from 0 */
    retVal = xn_interface_close(g_AX88796B_Driver.m_interfaceNumber);


    if (retVal == 0)
    {
        /* JRT - Wait not necessary since just did a HARD_CLOSE above
        xn_wait_pkts_output(RTP_TRUE, 60);
        */
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}

BOOL AX88796B_Driver::Bind(void)
{
    int retVal;

	debug_printf("ETH: Bind AX88796B Driver\r\n");

    retVal = xn_bind_AX88796B_mac(0);

    if (retVal != -1)
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}

